
/**
 * Module dependencies.
 */
var express = require('express');
var routes = require('./routes');
//var partials = require('express-partials'); //新版本express layout--partials支持
var http = require('http');
var path = require('path');
var MongoStore =require('connect-mongo')(express);// session用户信息存入数据库依赖
var config=require('./config');
var flash=require('connect-flash');     //flash通知模块
var async=require('async');
var sqlConn=require('./sqlserverConn');
var app = express();
//app.use(partials());

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(flash());
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.bodyParser({keepExtensions:true,uploadDir:'./public/images/user'}));//用户上传头像设置
app.use(express.cookieParser());//cookie解析中间件
app.use(express.session({       //提供会话支持
	secret:config.cookieSecret,
    cookie:{maxAge:1000*60*60*5}//10个小时

//	store:new MongoStore({
//		db:config.db
//	})
}))
app.use(function(req,res,next){	      //注册全局变量
	var err = req.flash('error');
    var success = req.flash('success');
	res.locals.user = req.session.user;
	res.locals.error= err.length ? err : null;
	res.locals.success = success.length ? success : null;
    req.session.url='http://'+config.server;
    res.locals.url=req.session.url;
    //公用
    if(!req.session.menu){
        async.waterfall([
            //加载培训机构列表
            function(callback){
                var sqlstr='SELECT * from  TrainingOrganization tog where tog.TrainingOrganization_Status=1'
                sqlConn(sqlstr,function(err,pxjg){
                    callback(err,pxjg)
                });

            },
            //加载培训动态菜单
            function(pxjg,callback){
                var sqlstr='SELECT * from  WebColumn wc where wc.WebColumn_ParentID=1'
                sqlConn(sqlstr,function(err,pxdt){
                    callback(err,pxjg,pxdt)
                });

            },
            //加载政策文件菜单
            function(pxjg,pxdt,callback){
                var sqlstr='SELECT * from  WebColumn wc where wc.WebColumn_ParentID=11'
                sqlConn(sqlstr,function(err,zcwj){
                    callback(err,pxjg,pxdt,zcwj)
                });
            },
            function(pxjg,pxdt,zcwj,callback){
                //var sqlstr='SELECT * from  SupplyDemand  sd where sd.SupplyDemand_Status=1 order by sd.SupplyDemand_ID desc'
                var sqlstr='select * from FriendlyLink fl left join FileContent fc on fl.FriendlyLink_ImageID=fc.FileContent_ID where fl.FriendlyLink_Status=1 '
                sqlConn(sqlstr,function(err,datasyqlj){
                    callback(err,pxjg,pxdt,zcwj,datasyqlj)
                });

            }

        ], function (err,pxjg,pxdt,zcwj,datasyqlj) {
            req.session.menu=true;
            req.session.pxjg=pxjg;
            req.session.pxdt=pxdt;
            req.session.zcwj=zcwj;
            req.session.datasyqlj=datasyqlj;
            res.locals.pxjg=pxjg;
            res.locals.pxdt=pxdt;
            res.locals.zcwj=zcwj;
            res.locals.datasyqlj=datasyqlj;
            next();
        });
    }else{
        res.locals.pxjg=req.session.pxjg;
        res.locals.pxdt=req.session.pxdt;
        res.locals.zcwj=req.session.zcwj;
        res.locals.datasyqlj=req.session.datasyqlj;
        next();
    }
});
app.use(function(req,res,next){
    var url=req.path;
    //var reg=url.indexOf('.')<0&&!req.session.user
    if(url=='/player1'){
        if(!req.session.user){
            req.flash('error','你没有登陆,请登陆后再试！！！');
            return res.redirect('back')
        }
    }
//    if(req.session.user){
//        if(url=='/reg'||url=='/'){
//            req.flash('error',res.__('warn').app_login);
//            return res.redirect('/findPartner')
//        }
//    }
    next();
})

app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});
routes(app);
